package com.gxuwz.endrskyweather;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.gxuwz.endrskyweather.adapter.CityManagerListAdapter;
import com.gxuwz.endrskyweather.viewmodel.CityManagerViewModel;
import java.util.ArrayList;

public class CityManagerFragment extends Fragment {

    private CityManagerListAdapter adapter;
    private CityManagerViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("CityManagerFragment", "onCreate called");
        viewModel = new ViewModelProvider(requireActivity()).get(CityManagerViewModel.class);
        Log.d("CityManagerFragment", "ViewModel initialized: " + viewModel);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_city_manager, container, false);
        Log.d("CityManagerFragment", "onCreateView called");
        
        // 初始化默认城市视图
        TextView tvDefaultCityName = view.findViewById(R.id.tv_default_city_name);
        TextView tvDefaultCityCode = view.findViewById(R.id.tv_default_city_code);
        TextView tvDefaultCityProvince = view.findViewById(R.id.tv_default_city_province);
        // 打印生命周期所有者信息
        Log.d("CityManagerFragment", "LifecycleOwner: " + getViewLifecycleOwner());
        Log.d("CityManagerFragment", "LiveData: " + viewModel.getDefaultCity());
        
        // 观察默认城市数据 - 使用getViewLifecycleOwner()确保生命周期安全
        viewModel.getDefaultCity().observe(getViewLifecycleOwner(), city -> {
            Log.d("CityManagerFragment", "Observer triggered with city: " + (city != null ? city.getAreaCn() : "null"));
            if (city != null) {
                tvDefaultCityName.setText(city.getAreaCn());
                tvDefaultCityCode.setText("城市代码：" + city.getAreaCode());
                tvDefaultCityProvince.setText("所属省份：" + city.getProvCn());
                Log.d("CityManagerFragment", "默认城市更新：" + city.getAreaCn());
            } else {
                tvDefaultCityName.setText("未设置默认城市");
                tvDefaultCityCode.setText("");
                tvDefaultCityProvince.setText("");
                Log.d("CityManagerFragment", "默认城市已清空");
            }
        });
        // 加载并观察默认城市数据
        viewModel.loadDefaultCity();
        // 初始化RecyclerView
        RecyclerView recyclerView = view.findViewById(R.id.rv_favorite_cities);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new CityManagerListAdapter(new ArrayList<>());
        recyclerView.setAdapter(adapter);
        // 观察非默认城市数据变化
        viewModel.getNonDefaultCities().observe(getViewLifecycleOwner(), cities -> {
            if (cities != null) {
                adapter.updateData(cities);
            }
        });
        // 初始加载非默认城市
        viewModel.loadNonDefaultCities();
        // 初始化FAB
        FloatingActionButton fabAddCity = view.findViewById(R.id.fab_add_city);
        fabAddCity.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), AddCityActivity.class);
            startActivity(intent);
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新所有数据
        viewModel.loadDefaultCity();
        viewModel.loadNonDefaultCities();
    }

}
